﻿Public Class PinyinKeyboard
    Inherits System.Windows.Forms.TextBox
    Private EscapeFlag As Boolean   ' Set when user presses the back slash \
    Private mTypingStyle As String
    Private LastCh As String

    Protected Overrides Function ProcessKeyMessage(ByRef m As System.Windows.Forms.Message) As Boolean
        ' Get out if this is not a KeyUp message
        If m.Msg <> 258 Then Return False
        Const Delay As Integer = 100
        ' Obtain the Keystroke character
        Dim KeyChar As Integer = m.WParam.ToInt32
        ' Process a keystroke
        Dim chList(48) As String
        Dim strKetQua As String
        Dim dv As New DataView()

        chList(0) = "ā" 'ā (ɑ̄) ē ī ō ū ǖ Ā Ē Ī Ō Ū Ǖ
        chList(1) = "Ā" 'AA
        chList(2) = "ē" ' ii
        chList(3) = "Ē"
        chList(4) = "ī" ' uu
        chList(5) = "Ī"
        chList(6) = "ō" ' N
        chList(7) = "Ō"
        chList(8) = "ū"
        chList(9) = "Ū" ' 
        chList(10) = "ǖ"
        chList(11) = "Ǖ" ' á (ɑ́) é í ó ú ǘ Á É Í Ó Ú Ǘ
        chList(12) = "á" 'T
        chList(13) = "Á"
        chList(14) = "é"
        chList(15) = "É"
        chList(16) = "í"
        chList(17) = "Í"
        chList(18) = "ó"
        chList(19) = "Ó"
        chList(20) = "ú" ' 	 LATIN SMALL LET
        chList(21) = "Ú"
        chList(22) = "ǘ"
        chList(23) = "Ǘ"
        chList(24) = "ǎ" ' ǎ (ɑ̌) ě ǐ ǒ ǔ ǚ Ǎ Ě Ǐ Ǒ Ǔ Ǚ
        chList(25) = "Ǎ" ' 
        chList(26) = "ě"
        chList(27) = "Ě" ' 
        chList(28) = "ǐ" ' 
        chList(29) = "Ǐ" ' 
        chList(30) = "ǒ" ' 
        chList(31) = "Ǒ" ' 
        chList(32) = "ǔ" ' 
        chList(33) = "Ǔ" ' 
        chList(34) = "ǚ" ' 
        chList(35) = "Ǚ" ' 
        chList(36) = "à" ' à (ɑ̀) è ì ò ù ǜ À È Ì Ò Ù Ǜ
        chList(37) = "À" ' 
        chList(38) = "è"
        chList(39) = "È" ' 
        chList(40) = "ì" ' 
        chList(41) = "Ì" ' 
        chList(42) = "ò" ' 
        chList(43) = "Ò" ' 
        chList(44) = "ù" ' 
        chList(45) = "Ù" ' 
        chList(46) = "ǜ" ' 
        chList(47) = "Ǜ" ' 

        If Len(Me.Text) <= 0 Then Exit Function
        'If mTypingStyle = "Pali" Then
        Select Case KeyChar
            Case 49 ' 1
                If Mid(Me.Text, Me.SelectionStart, 1) = "a" Then ' 'ā ē ī ō ū ǖ Ā Ē Ī Ō Ū Ǖ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(0)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "A" Then ' Ā
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(1)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "e" Then ' ē
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(2)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "E" Then ' Ē
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(3)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "i" Then ' ī
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(4)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "I" Then ' Ī
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(5)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "o" Then ' ō
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(6)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "O" Then ' Ō
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(7)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "u" Then ' ū
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(8)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "U" Then ' Ū
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(9)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "ū" Then ' ǖ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(10)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "Ū" Then ' Ǖ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(11)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
                ' ------------------------------
                ' NUM 2
                ' ------------------------------
            Case 50 ' 2
                If Mid(Me.Text, Me.SelectionStart, 1) = "a" Then ' á é í ó ú ǘ Á É Í Ó Ú Ǘ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(12)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "A" Then ' Á
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(13)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "e" Then ' é
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(14)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "E" Then ' É
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(15)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "i" Then ' í
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(16)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "I" Then ' Í
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(17)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "o" Then ' ó
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(18)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "O" Then ' Ó
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(19)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "u" Then ' ú
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(20)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "U" Then ' Ú
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(21)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "ú" Then ' ǘ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(22)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "Ú" Then ' Ǘ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(23)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
                ' -------------------------
                ' NUM 3
                ' -------------------------
            Case 51 ' 3
                If Mid(Me.Text, Me.SelectionStart, 1) = "a" Then 'ǎ ě ǐ ǒ ǔ ǚ Ǎ Ě Ǐ Ǒ Ǔ Ǚ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(24)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "A" Then ' Ǎ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(25)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "e" Then ' ě
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(26)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "E" Then ' Ě
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(27)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "i" Then ' ǐ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(28)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "I" Then ' Ǐ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(29)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "o" Then ' ǒ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(30)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "O" Then ' Ǒ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(31)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "u" Then ' ǔ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(32)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "U" Then ' Ǔ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(33)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "ǔ" Then ' ǚ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(34)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "Ǔ" Then ' Ǚ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(35)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
                ' ----------------------
                ' NUM 5
                ' --------------------------
            Case 52 ' 2
                If Mid(Me.Text, Me.SelectionStart, 1) = "a" Then ' à è ì ò ù ǜ À È Ì Ò Ù Ǜ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(36)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "A" Then ' À
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(37)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "e" Then ' è
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(38)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "E" Then ' È
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(39)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "i" Then ' ì
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(40)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "I" Then ' Ì
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(41)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "o" Then ' ò
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(42)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "O" Then ' Ò
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(43)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "u" Then ' ù
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(44)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "U" Then ' Ù
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(45)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "ù" Then ' ǜ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(46)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                ElseIf Mid(Me.Text, Me.SelectionStart, 1) = "Ù" Then ' Ǜ
                    strKetQua = Mid(Me.Text, 1, Me.SelectionStart - 1) & chList(47)
                    Clipboard.SetDataObject(strKetQua)
                    Me.Clear()
                    Me.Paste()
                    Return True
                End If
        End Select
        'End If
        '-------------------------------
        ' KHU VUC TIM KIEM
        ' ------------------------------

        Try
            If KeyChar <> 32 And Me.Text <> "" Then
                dv.Table = frmMain.ds.Tables(frmMain.DictTable)
                If frmMain.isWordSelect = True Then
                    dv.RowFilter = "Words like '%" & Me.Text & "%'"
                Else
                    dv.RowFilter = "Transcription like '%" & Me.Text & "%'"
                End If
                frmMain.listWords.DataSource = dv

                If KeyChar = Keys.Enter Then
                    If frmMain.isWordSelect = True Then
                        frmMain.SearchWords(frmMain.DictTable, "Words", Me.Text)
                    Else
                        frmMain.SearchWords(frmMain.DictTable, "Transcription", Me.Text)
                    End If
                End If
            End If
        Catch
            Me.Text = Me.Text
        Finally

        End Try

        EscapeFlag = False  ' Reset the Escape Flag
    End Function
End Class
